<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/8
 * Time: 17:33
 * 审核管理
 */
namespace app\admin\controller;
use think\Db;
class Verify extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $where = array();
            $params = input('param.');
            if(isset($params['s_dl_status']) && $params['s_dl_status'] != '')
            {
                $where['s_dl_status'] = $params['s_dl_status'];
            }
            if (isset($params['key']) && $params['key'] != '') {
                $where['a.s_dl_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $where['a.s_dl_status'] = array('neq',1);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shops')->alias('a')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('shops')->alias('a')
                ->page($Nowpage, $limits)
                ->where($where)
                ->select();
            foreach ($lists as $key => &$vo) {
                $sheng = Db::table('up_area')->where('district_id', $vo['s_dl_sheng'])->value('district');
                $shi = Db::table('up_area')->where('district_id', $vo['s_dl_shi'])->value('district');
                $vo['address'] = $sheng . $shi;
                if($vo['s_dl_status']==2){
                    $status_name = '待审核';
                }elseif ($vo['s_dl_status']==3){
                    $status_name = '审核通过';
                }elseif ($vo['s_dl_status']==4){
                    $status_name = '审核不通过';
                }
                $vo['status_name'] = $status_name;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 拒绝
     */
    public function jujue(){
        $res = Db::table('up_shops')->where('s_id',input('param.id'))->setField('s_dl_status',4);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * @return mixed
     * 详情
     */
    public function detail(){
        $where['s_id'] = input('param.id');
        $lists = Db::name('shops')->alias('a')
            ->where($where)
            ->find();
            $sheng = Db::table('up_area')->where('district_id', $lists['s_dl_sheng'])->value('district');
            $shi = Db::table('up_area')->where('district_id', $lists['s_dl_shi'])->value('district');
            $lists['address'] = $sheng . $shi;
            if($lists['s_dl_status']==2){
                $status_name = '待审核';
            }elseif ($lists['s_dl_status']==3){
                $status_name = '审核通过';
            }elseif ($lists['s_dl_status']==4){
                $status_name = '审核不通过';
            }
            $lists['status_name'] = $status_name;
            $this->assign('list',$lists);
            return $this->fetch();
    }

    /***
     * @return mixed
     * 审核通过
     */
    public function sheng(){
        //todo  缺少短信模板
        if(request()->isPost()){
            $have = Db::table('up_dl')
                ->where('dl_account',input('param.account'))
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '账号重复']);
            }
            if(strstr(input('param.account'), ',')){
                return json(['code' => 100, 'msg' => '账号不能有’,‘逗号']);
            }
            if(strstr(input('param.password'), ',')){
                return json(['code' => 100, 'msg' => '密码不能有’,‘逗号']);
            }
            $data1['s_dl_status'] = 3;
            $data1['s_dl_sh_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_shops')->where('s_id',input('param.id'))->update($data1);
            $info = Db::table('up_shops')->where('s_id',input('param.id'))->find();
            $data['dl_name'] = $info['s_dl_name'];
            $data['dl_phone'] = $info['s_dl_phone'];
            $data['dl_intro'] = $info['s_dl_content'];
            $data['dl_account'] = input('param.account');
            $data['dl_password'] = md5(input('param.password'));
            $data['password'] = input('param.password');
            $data['dl_sheng'] = $info['s_dl_sheng']?$info['s_dl_sheng']:'';
            $data['dl_shi'] = $info['s_dl_shi']?$info['s_dl_shi']:'';
            $data['dl_shi'] = $info['s_dl_qu']?$info['s_dl_qu']:'';
            $data['dl_time'] = date('Y-m-d H:i:s',time());
            $data['dl_level'] = $info['s_dl_level'];
            $data['d_f_id'] = $info['s_id'];
            $res  = Db::table('up_dl')->insertGetId($data);
            if($res){
                $one = input('param.account');
                $two = input('param.password');
                allInNotice('代理商审核通过',"恭喜您，申请的代理商已通过申请，账号：{$one}，密码：{$two}，为了保证您的账号安全请尽快修改密码。",3,$info['s_id']);
//                $templateid = config('yzx')['dlpass'];
//                try{
//                    YzxSms("$one,$two", $info['s_dl_phone'], $templateid);//发送审核通过通知
//                }catch(\Exception $e){
//
//                }
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $where['s_id'] = input('param.id');
        $lists = Db::name('shops')->alias('a')
            ->where($where)
            ->find();
        $sheng = Db::table('up_area')->where('district_id', $lists['s_dl_sheng'])->value('district');
        $shi = Db::table('up_area')->where('district_id', $lists['s_dl_shi'])->value('district');
        $lists['address'] = $sheng . $shi;
        $this->assign('list',$lists);
        $this->assign('id',input('param.id'));
        return $this->fetch();
    }
}